/**
 * @file 008.和大于等于target的最短子数组.cc
 * @author snow-tyan (zziywang@163.com)
 * @brief {Life is too short to learn cpp.}
 * @version 0.1
 * @date 2021-12-07
 *
 * @copyright Copyright (c) 2021
 *
 */

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

template <typename T>
void print(const T &t)
{
    typename T::const_iterator it = t.begin();
    for (; it != t.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;
}

class Solution
{
public:
    int minSubArrayLen(int target, vector<int> &nums)
    {
        // 滑窗
        int n = nums.size();
        int res = n + 1;
        int i = 0, j = 0, sum = 0;
        while (j < n) {
            sum += nums[j];
            while (sum >= target){
                res = min(res, j - i + 1);
                sum -= nums[i++];
            }
            ++j;
        }
        return res == n + 1 ? 0 : res;
    }
};

int main()
{
    vector<int> nums1 = {2, 3, 1, 2, 4, 3};
    vector<int> nums2 = {1, 4, 4};
    vector<int> nums3 = {1, 1, 1, 1, 1, 1, 1, 1};
    cout << Solution().minSubArrayLen(7, nums1) << endl;
    cout << Solution().minSubArrayLen(4, nums2) << endl;
    cout << Solution().minSubArrayLen(11, nums3) << endl;
}